package com.lovi.carousel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovi.carousel.mapper.CarouselMapper;
import com.lovi.carousel.service.CarouselService;
import com.lovi.pojo.Carousel;
import com.lovi.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 查询优先级最高的4条轮播图数据
     * @return
     */
    @Cacheable(value = "list.carousel",key = "#root.methodName")
    @Override
    public Object list() {
        Integer limit = 6;//最多四条
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        //排序
        wrapper.orderByDesc("priority");

        //配置分页，因为只显示前四条
        IPage<Carousel> iPage = new Page<>(1,limit); //实例化了一个分页对象,显示第一页，第一页最多4个
        IPage<Carousel> page = carouselMapper.selectPage(iPage,wrapper);//通过分页查询方法得到页面结果
        List<Carousel> carousels = page.getRecords();//从页面结果中拿到具体的对象集合

        return R.ok(carousels);
    }
}
